home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / gdevpng.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  8.9 KB  |  279 lines

  1. /* Copyright (C) 1995, 2000 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: gdevpng.c,v 1.3 2000/09/19 19:00:21 lpd Exp $ */
  20. /* PNG (Portable Network Graphics) Format.  Pronounced "ping". */
  21. /* lpd 1999-09-24: changes PNG_NO_STDIO to PNG_NO_CONSOLE_IO for libpng
  22.    versions 1.0.3 and later. */
  23. /* lpd 1999-07-01: replaced remaining uses of gs_malloc and gs_free with
  24.    gs_alloc_bytes and gs_free_object. */
  25. /* lpd 1999-03-08: changed png.h to png_.h to allow compiling with only
  26.    headers in /usr/include, no source code. */
  27. /* lpd 1997-07-20: changed from using gs_malloc/png_xxx_int to png_create_xxx
  28.  * for allocating structures, and from gs_free to png_write_destroy for
  29.  * freeing them. */
  30. /* lpd 1997-5-7: added PNG_LIBPNG_VER conditional for operand types of
  31.  * dummy png_push_fill_buffer. */
  32. /* lpd 1997-4-13: Added PNG_NO_STDIO to remove library access to stderr. */
  33. /* lpd 1997-3-14: Added resolution (pHYs) to output. */
  34. /* lpd 1996-6-24: Added #ifdef for compatibility with old libpng versions. */
  35. /* lpd 1996-6-11: Edited to remove unnecessary color mapping code. */
  36. /* lpd (L. Peter Deutsch) 1996-4-7: Modified for libpng 0.88. */
  37. /* Original version by Russell Lang 1995-07-04 */
  38.  
  39. #include "gdevprn.h"
  40. #include "gdevpccm.h"
  41. #include "gscdefs.h"
  42.  
  43. #define PNG_INTERNAL
  44. /*
  45.  * libpng versions 1.0.3 and later allow disabling access to the stdxxx
  46.  * files while retaining support for FILE * I/O.
  47.  */
  48. #define PNG_NO_CONSOLE_IO
  49. /*
  50.  * Earlier libpng versions require disabling FILE * I/O altogether.
  51.  * This produces a compiler warning about no prototype for png_init_io.
  52.  * The right thing will happen at link time, since the library itself
  53.  * is compiled with stdio support.  Unfortunately, we can't do this
  54.  * conditionally depending on PNG_LIBPNG_VER, because this is defined
  55.  * in png.h.
  56.  */
  57. /*#define PNG_NO_STDIO*/
  58. #include "png_.h"
  59.  
  60. /* ------ The device descriptors ------ */
  61.  
  62. /*
  63.  * Default X and Y resolution.
  64.  */
  65. #define X_DPI 72
  66. #define Y_DPI 72
  67.  
  68. private dev_proc_print_page(png_print_page);
  69.  
  70. /* Monochrome. */
  71.  
  72. const gx_device_printer gs_pngmono_device =
  73. prn_device(prn_std_procs, "pngmono",
  74.        DEFAULT_WIDTH_10THS, DEFAULT_HEIGHT_10THS,
  75.        X_DPI, Y_DPI,
  76.        0, 0, 0, 0,        /* margins */
  77.        1, png_print_page);
  78.  
  79. /* 4-bit planar (EGA/VGA-style) color. */
  80.  
  81. private const gx_device_procs png16_procs =
  82. prn_color_procs(gdev_prn_open, gdev_prn_output_page, gdev_prn_close,
  83.         pc_4bit_map_rgb_color, pc_4bit_map_color_rgb);
  84. const gx_device_printer gs_png16_device =
  85. prn_device(png16_procs, "png16",
  86.        DEFAULT_WIDTH_10THS, DEFAULT_HEIGHT_10THS,
  87.        X_DPI, Y_DPI,
  88.        0, 0, 0, 0,        /* margins */
  89.        4, png_print_page);
  90.  
  91. /* 8-bit (SuperVGA-style) color. */
  92. /* (Uses a fixed palette of 3,3,2 bits.) */
  93.  
  94. private const gx_device_procs png256_procs =
  95. prn_color_procs(gdev_prn_open, gdev_prn_output_page, gdev_prn_close,
  96.         pc_8bit_map_rgb_color, pc_8bit_map_color_rgb);
  97. const gx_device_printer gs_png256_device =
  98. prn_device(png256_procs, "png256",
  99.        DEFAULT_WIDTH_10THS, DEFAULT_HEIGHT_10THS,
  100.        X_DPI, Y_DPI,
  101.        0, 0, 0, 0,        /* margins */
  102.        8, png_print_page);
  103.  
  104. /* 8-bit gray */
  105.  
  106. private const gx_device_procs pnggray_procs =
  107. prn_color_procs(gdev_prn_open, gdev_prn_output_page, gdev_prn_close,
  108.           gx_default_gray_map_rgb_color, gx_default_gray_map_color_rgb);
  109. const gx_device_printer gs_pnggray_device =
  110. {prn_device_body(gx_device_printer, pnggray_procs, "pnggray",
  111.          DEFAULT_WIDTH_10THS, DEFAULT_HEIGHT_10THS,
  112.          X_DPI, Y_DPI,
  113.          0, 0, 0, 0,    /* margins */
  114.          1, 8, 255, 0, 256, 0, png_print_page)
  115. };
  116.  
  117. /* 24-bit color. */
  118.  
  119. private const gx_device_procs png16m_procs =
  120. prn_color_procs(gdev_prn_open, gdev_prn_output_page, gdev_prn_close,
  121.         gx_default_rgb_map_rgb_color, gx_default_rgb_map_color_rgb);
  122. const gx_device_printer gs_png16m_device =
  123. prn_device(png16m_procs, "png16m",
  124.        DEFAULT_WIDTH_10THS, DEFAULT_HEIGHT_10THS,
  125.        X_DPI, Y_DPI,
  126.        0, 0, 0, 0,        /* margins */
  127.        24, png_print_page);
  128.  
  129.  
  130. /* ------ Private definitions ------ */
  131.  
  132. /* Write out a page in PNG format. */
  133. /* This routine is used for all formats. */
  134. private int
  135. png_print_page(gx_device_printer * pdev, FILE * file)
  136. {
  137.     gs_memory_t *mem = pdev->memory;
  138.     int raster = gdev_prn_raster(pdev);
  139.  
  140.     /* PNG structures */
  141.     byte *row = gs_alloc_bytes(mem, raster, "png raster buffer");
  142.     png_struct *png_ptr =
  143.     png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
  144.     png_info *info_ptr =
  145.     png_create_info_struct(png_ptr);
  146.     int height = pdev->height;
  147.     int depth = pdev->color_info.depth;
  148.     int y;
  149.     int code;            /* return code */
  150.     const char *software_key = "Software";
  151.     char software_text[256];
  152.     png_text text_png;
  153.  
  154.     if (row == 0 || png_ptr == 0 || info_ptr == 0) {
  155.     code = gs_note_error(gs_error_VMerror);
  156.     goto done;
  157.     }
  158.     /* set error handling */
  159.     if (setjmp(png_ptr->jmpbuf)) {
  160.     /* If we get here, we had a problem reading the file */
  161.     code = gs_note_error(gs_error_VMerror);
  162.     goto done;
  163.     }
  164.     code = 0;            /* for normal path */
  165.     /* set up the output control */
  166.     png_init_io(png_ptr, file);
  167.  
  168.     /* set the file information here */
  169.     info_ptr->width = pdev->width;
  170.     info_ptr->height = pdev->height;
  171.     /* resolution is in pixels per meter vs. dpi */
  172.     info_ptr->x_pixels_per_unit =
  173.     (png_uint_32) (pdev->HWResolution[0] * (100.0 / 2.54));
  174.     info_ptr->y_pixels_per_unit =
  175.     (png_uint_32) (pdev->HWResolution[1] * (100.0 / 2.54));
  176.     info_ptr->phys_unit_type = PNG_RESOLUTION_METER;
  177.     info_ptr->valid |= PNG_INFO_pHYs;
  178.     switch (depth) {
  179.     case 24:
  180.         info_ptr->bit_depth = 8;
  181.         info_ptr->color_type = PNG_COLOR_TYPE_RGB;
  182.         break;
  183.     case 8:
  184.         info_ptr->bit_depth = 8;
  185.         if (gx_device_has_color(pdev))
  186.         info_ptr->color_type = PNG_COLOR_TYPE_PALETTE;
  187.         else
  188.         info_ptr->color_type = PNG_COLOR_TYPE_GRAY;
  189.         break;
  190.     case 4:
  191.         info_ptr->bit_depth = 4;
  192.         info_ptr->color_type = PNG_COLOR_TYPE_PALETTE;
  193.         break;
  194.     case 1:
  195.         info_ptr->bit_depth = 1;
  196.         info_ptr->color_type = PNG_COLOR_TYPE_GRAY;
  197.         /* invert monocrome pixels */
  198.         png_set_invert_mono(png_ptr);
  199.         break;
  200.     }
  201.  
  202.     /* set the palette if there is one */
  203.     if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE) {
  204.     int i;
  205.     int num_colors = 1 << depth;
  206.     gx_color_value rgb[3];
  207.  
  208.     info_ptr->palette =
  209.         (void *)gs_alloc_bytes(mem, 256 * sizeof(png_color),
  210.                    "png palette");
  211.     if (info_ptr->palette == 0) {
  212.         code = gs_note_error(gs_error_VMerror);
  213.         goto done;
  214.     }
  215.     info_ptr->num_palette = num_colors;
  216.     info_ptr->valid |= PNG_INFO_PLTE;
  217.     for (i = 0; i < num_colors; i++) {
  218.         (*dev_proc(pdev, map_color_rgb)) ((gx_device *) pdev,
  219.                           (gx_color_index) i, rgb);
  220.         info_ptr->palette[i].red = gx_color_value_to_byte(rgb[0]);
  221.         info_ptr->palette[i].green = gx_color_value_to_byte(rgb[1]);
  222.         info_ptr->palette[i].blue = gx_color_value_to_byte(rgb[2]);
  223.     }
  224.     }
  225.     /* add comment */
  226.     sprintf(software_text, "%s %d.%02d", gs_product,
  227.         (int)(gs_revision / 100), (int)(gs_revision % 100));
  228.     text_png.compression = -1;    /* uncompressed */
  229.     text_png.key = (char *)software_key;    /* not const, unfortunately */
  230.     text_png.text = software_text;
  231.     text_png.text_length = strlen(software_text);
  232.     info_ptr->text = &text_png;
  233.     info_ptr->num_text = 1;
  234.  
  235.     /* write the file information */
  236.     png_write_info(png_ptr, info_ptr);
  237.  
  238.     /* don't write the comments twice */
  239.     info_ptr->num_text = 0;
  240.     info_ptr->text = NULL;
  241.  
  242.     /* Write the contents of the image. */
  243.     for (y = 0; y < height; y++) {
  244.     gdev_prn_copy_scan_lines(pdev, y, row, raster);
  245.     png_write_rows(png_ptr, &row, 1);
  246.     }
  247.  
  248.     /* write the rest of the file */
  249.     png_write_end(png_ptr, info_ptr);
  250.  
  251.     /* if you alloced the palette, free it here */
  252.     gs_free_object(mem, info_ptr->palette, "png palette");
  253.  
  254.   done:
  255.     /* free the structures */
  256.     png_destroy_write_struct(&png_ptr, &info_ptr);
  257.     gs_free_object(mem, row, "png raster buffer");
  258.  
  259.     return code;
  260. }
  261.  
  262. /*
  263.  * Patch around a static reference to a never-used procedure.
  264.  * This could be avoided if we were willing to edit pngconf.h to
  265.  *      #undef PNG_PROGRESSIVE_READ_SUPPORTED
  266.  */
  267. #ifdef PNG_PROGRESSIVE_READ_SUPPORTED
  268. #  if PNG_LIBPNG_VER >= 95
  269. #    define PPFB_LENGTH_T png_size_t
  270. #  else
  271. #    define PPFB_LENGTH_T png_uint_32
  272. #  endif
  273. void
  274. png_push_fill_buffer(png_structp png_ptr, png_bytep buffer,
  275.              PPFB_LENGTH_T length)
  276. {
  277. }
  278. #endif
  279.